
$(function() {

	Tip.emp('采购员');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("入库员：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
	
	Tip.supplier();
	
});

var Receive = {

	popup: function (e, p2, id) {
		$.cookie('id_', id);

		$.cookie('status_', status);
		
		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'material.php?do=Receive.edit&id=' + id;
		var c2 = 'material.php?do=Receive.view&id=' + id;
		var c3 = 'material.php?do=Receive.split&id=' + id;

		var a1 = ['编辑信息', 'url', 'Receive.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '800x500'];
		var a3 = ['拆分信息', 'url', 'Receive.dialog_split_', '800x500'];
		
		var s = c1 + ", " + c2 + ", " + c3;

		return this.popup_(e, [a1, a2, a3], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'material.php?do=Receive.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Receive.dialog_ss', '400x200'];
		
		s += ", " + ss;
		
		a.push(bb);
		
		return popup(e, a, s);
	},
	
	dialog_ss: function (url, title, xy) {
		result = Common.result();
		$.getJSON('material.php?do=Receive.audit&item=188', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=Receive.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Receive.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
		
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
		
						var form = $("#form_edit")[0];
		
						var body = {};
						body.id = form.id.value;
						body.emp = form.emp.value;						
						body.date = form.date.value;
		
						body.supplier = form.supplier.value;
						body.number = form.number.value;
						body.purchase = form.purchase.value;
		
						body.content = form.content.value;
						body.status = form.status.value;
		
						$(this).dialog("close");
		
						var url = 'material.php?do=Receive.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
		
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
		
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	expunge_: function (form) {
		$.getJSON('material.php?do=Receive.audit&item=52', function(json) {
			if (json.status == 1) {
				expunge(form);
			} else {
				_error();
			}
		});
	},
	
	dialog_split_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=Receive.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
						
						var cc = Receive._cc();
		
						var form = $("#form_edit")[0];
						
						var body = {};
						
						body.id = form.id.value;
						body.number = form.number.value;
						body.cc = cc;
						
						var url = 'material.php?do=Receive.splitPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");
		
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
				
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=Receive.delete' + p2;
		var c2 = 'material.php?do=Receive.batch_total';
		var c3 = 'material.php?do=Receive.bill' + p2;
		
		var c9 = 'material.php?do=Receive.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['金额更新', 'url', 'Receive.dialog_batch_total', '800x450'];
		var a3 = ['对帐单', 'url', 'Receive.dialog_batch_bill', '1024x768'];
		
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2 + ", " + c3 + ", " + c9;

		return popup(e, [a1, a2, a3, a9], s);
	},
	
	dialog_batch_total: function (url, title, xy) {
		
		var result = Common.result();
		var c = new Array();
		var d = new Array();
		$("input[name^='c_']").each(function() {
			if ($(this).attr("checked")) {
				var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
				c.push(s);
				
				var n = $(this).attr("name");
				var a = n.split('_');
				d.push(a[3]);
			}
		});
		
		var body = c.join("&");
		
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},					
			
			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var cc = Receive._cc();

				var form = $("#form_edit")[0];
				
				var body = {};
				body.cc = cc;
				
				var url = 'material.php?do=Receive.batch_totalPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
				
				$(this).dialog("close");

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		var view = '#dialog_edit';
		$.get(url, body, function(data) {
			$(view).html(data);
		});

		var regex = /^(\d+)(x)(\d+)$/;
		if (regex.test(xy)) {
			var cc = xy.split("x");
			var width = parseInt(cc[0]);
			var height = parseInt(cc[1]);
			$(view).dialog('option', 'height', height);
		} else {
			var width = parseInt(xy);
		}

		$(view).dialog('option', 'title', title);
		$(view).dialog('option', 'width', width);
		$(view).dialog('open');
	},
	
	dialog_batch_bill: function (url, title, xy) {
				
		var c = new Array();
		var d = new Array();
		$("input[name^='c_']").each(function() {
			if ($(this).attr("checked")) {
				var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
				c.push(s);
				
				var n = $(this).attr("name");
				var a = n.split('_');
				d.push(a[2]);
			}
		});
		
		var cc = '';
		for (i = 0; i < d.length; i++) {
			if (cc.length > 0 && cc != d[i]) {
				alert("操作有误，必须选择同一供应商的进货单！");
				return false;
			}
			
			cc = d[i];
		}
		
		var body = c.join("&");
		
		// alert(body);
		
		// var url = 'express.php?do=Invoice.view&v=1&id=' + id; 	 

		url = url + "&" + body;
		// alert(url);
        jswin(url, 'print', '800x600');
	},
	
	_cc: function () {
		var c = new Array();
		$("input[name^='b_']").each(function() {
			if ($(this).attr("checked")) {
				c.push($(this).val());
			}
		});
		
		return c;
	}
}
